package com.dg.gaming.serverloadbalancer;

import java.util.concurrent.*;

import com.dg.dgrmi.net.*;
import com.dg.gaming.communication.*;
import net.sf.ehcache.*;
import org.xsocket.connection.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class Communicator implements
        ICommunicator, IServerHendler, IDGRMIServerListener {
    private static final String CACH_NAME = "clientCach";
    private ArrayBlockingQueue<IServerHendler> queue;
    private Cache _cache = null;

    public Communicator(int queueSize) throws Exception {
        super();
        queue = new ArrayBlockingQueue<IServerHendler>(queueSize);
        CacheManager singletonManager = CacheManager.create();
        singletonManager.addCache(CACH_NAME);
        _cache = singletonManager.getCache(CACH_NAME);

    }


    public IServerHendler registerMe(IServerHendler server) {
        queue.add(server);
        return this;
    }

    public void ping() {
        System.out.println("ping");
    }

    public void sendData(byte[] bytes, String sessionID) throws Exception {
        Element element = _cache.get(sessionID);
        if (element != null) {
            INonBlockingConnection conn = (INonBlockingConnection) element.
                                          getValue();

        }
    }

    public void clientConnected(INonBlockingConnection socket) {
        Element element = new Element(socket.getId(), socket);
        _cache.put(element);
    }

    public void clientDisconnected(INonBlockingConnection socket) {
        _cache.remove(socket.getId());
    }


}
